package com.twinkle.operator;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class ClsOperator {
    private ClsManager cm;

    public ClsOperator() {
        cm = new ClsManager();
    }

    /*** 查看所有班级 */
    private void showAllCls() {
        List<Cls> clsList = cm.selectClsAll();
        if (clsList.size() > 0) {
            clsList.stream().forEach(System.out::println);
        } else {
            System.out.println("当前无班级");
        }
    }

    /*** 查看班级学生 */
    private void showStuByCls() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查看班级的编号:");
        String clsNo = scanner.nextLine();
        Integer clsId = cm.selectClsByClsNo(clsNo);
        if (clsId != null) {
            List<Stu> stuList = cm.selectStuByCls(clsId);
            if (stuList.size() == 0) {
                System.out.println("班级" + clsNo + "暂无学生");
            } else {
                stuList.stream().forEach(System.out::println);
            }
        } else {
            System.out.println("班级:" + clsNo + "不存在");
        }
    }

    /*** 开班 */
    private void startCls() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入新开班级编号:");
        String clsNo = scanner.nextLine();
        Integer clsId = cm.selectClsByClsNo(clsNo);
        if (clsId == null) {
            System.out.print("请输入新开班级名称:");
            String clsName = scanner.nextLine();
            Cls cls = new Cls();
            cls.setClsNo(clsNo);
            cls.setClsName(clsName);
            cls.setClsBeginTime(new Date());
            boolean rt = cm.openCls(cls);
            if (rt) {
                System.out.println("班级:" + clsNo + "开课成功");
            } else {
                System.out.println("班级:" + clsNo + "开课失败");
            }
        } else {
            System.out.println("班级:" + clsNo + "已存在");
        }
    }

    /*** 结班 */
    private void endCls() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要结课班级的编号:");
        String clsNo = scanner.nextLine();
        Integer clsId = cm.selectClsByClsNo(clsNo);
        if (clsId != null) {
            boolean rt = cm.closeCls(clsId);
            if (rt) {
                System.out.println("班级:" + clsNo + "结班成功");
            } else {
                System.out.println("班级:" + clsNo + "结班失败");
            }
        } else {
            System.out.println("班级:" + clsNo + "不存在");
        }
    }

    /*** 入学 */
    private void entrance() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要入学的班级:");
        String clsNo = scanner.nextLine();
        Integer clsId = cm.selectClsByClsNo(clsNo);
        if (clsId != null) {
            System.out.print("请输入学号：");
            String stuNo = scanner.nextLine();
            System.out.print("请输入姓名：");
            String stuName = scanner.nextLine();
            Stu stu = new Stu();
            stu.setClsId(clsId);
            stu.setStuName(stuName);
            stu.setStuNo(stuNo);
            boolean rt = cm.addStu(stu);
            if (rt) {
                System.out.println("学生:" + stu.getStuName() + "入学成功");
            } else {
                System.out.println("学生:" + stu.getStuName() + "入学失败");
            }
        } else {
            System.out.println("班级:" + clsNo + "不存在");
        }
    }

    /*** 系统操作菜单 */
    public void service() {
        boolean ifExist = false;
        do {
            System.out.println("***********************************************");
            System.out.println("1、开班\t2、结班\t3、入学");
            System.out.println("4、查询所有班级\t5、查看班级学生\t6、退出");
            System.out.println("***********************************************");
            System.out.print("请选择：");
            Scanner scanner = new Scanner(System.in);
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    startCls();
                    break;
                case "2":
                    endCls();
                    break;
                case "3":
                    entrance();
                    break;
                case "4":
                    showAllCls();
                    break;
                case "5":
                    showStuByCls();
                    break;
                case "6":
                    ifExist = true;
                    break;
                default:
                    System.out.println("无效输入");
                    break;
            }
        }
        while (!ifExist);
    }
}
